home *** CD-ROM | disk | FTP | other *** search
/ The Fatted Calf / The Fatted Calf.iso / Applications / Graphics / GraphicsWorkshop / Source / Converters / gif.m < prev    next >
Encoding:
Text File  |  1992-03-09  |  18.2 KB  |  734 lines

  1. /*
  2.  * xvgif.c  -  GIF loading code for 'xv'.  Based strongly on...
  3.  *
  4.  * gif2ras.c - Converts from a Compuserve GIF (tm) image to a Sun Raster image.
  5.  *
  6.  * Copyright (c) 1988, 1989 by Patrick J. Naughton
  7.  *
  8.  * Author: Patrick J. Naughton
  9.  * naughton@wind.sun.com
  10.  *
  11.  * Permission to use, copy, modify, and distribute this software and its
  12.  * documentation for any purpose and without fee is hereby granted,
  13.  * provided that the above copyright notice appear in all copies and that
  14.  * both that copyright notice and this permission notice appear in
  15.  * supporting documentation.
  16.  *
  17.  * This file is provided AS IS with no warranties of any kind.  The author
  18.  * shall have no liability with respect to the infringement of copyrights,
  19.  * trade secrets or any patents by this file or any part thereof.  In no
  20.  * event will the author be liable for any lost revenue or profits or
  21.  * other special, indirect and consequential damages.
  22.  *
  23.  */
  24.  
  25. /*
  26.  * Copyright 1989, 1990 by the University of Pennsylvania
  27.  *
  28.  * Permission to use, copy, and distribute for non-commercial purposes,
  29.  * is hereby granted without fee, providing that the above copyright
  30.  * notice appear in all copies and that both the copyright notice and this
  31.  * permission notice appear in supporting documentation.
  32.  *
  33.  * The software may be modified for your own purposes, but modified versions
  34.  * may not be distributed.
  35.  *
  36.  * This software is provided "as is" without any express or implied warranty.
  37.  */
  38.  
  39. #import <stdio.h>
  40. #import <string.h>
  41. #import <stdlib.h>
  42. #import <streams/streams.h>
  43. #import <appkit/NXBitmapImageRep.h>
  44. #import "NXBitmapImageRepControl.h"
  45. #import "ImageControl.h"
  46. #import "gif.h"
  47. #include "libgif.h"
  48.  
  49. @implementation GIF
  50.  
  51.     typedef long int        count_int;
  52.     typedef unsigned char    byte;
  53.  
  54.     static int                curx, cury;
  55.     static long            CountDown;
  56.     static byte            bw[2] = {0, 0xff};
  57.  
  58.     int            BitOffset = 0,                /* Bit Offset of next code */
  59.                 XC = 0, YC = 0,            /* Output X and Y coords of current pixel */
  60.                 Pass = 0,                    /* Used by output routine if interlaced pic */
  61.                 OutCount = 0,                /* Decompressor output 'stack count' */
  62.                 RWidth, RHeight,            /* screen dimensions */
  63.                 Width, Height,            /* image dimensions */
  64.                 LeftOfs, TopOfs,            /* image offset */
  65.                 BitsPerPixel,                /* Bits per pixel, read from GIF header */
  66.                 BytesPerScanline,            /* bytes per scanline in output raster */
  67.                 ColorMapSize,            /* number of colors */
  68.                 Background,                /* background color */
  69.                 CodeSize,                /* Code size, read from GIF header */
  70.                 InitCodeSize,                /* Starting code size, used during Clear */
  71.                 Code,                    /* Value returned by ReadCode */
  72.                 MaxCode,                /* limiting value for current code size */
  73.                 ClearCode,                /* GIF clear code */
  74.                 EOFCode,                /* GIF end-of-information code */
  75.                 CurCode, OldCode, InCode,    /* Decompressor variables */
  76.                 FirstFree,                /* First free code, generated per GIF spec */
  77.                 FreeCode,                /* Decompressor,next free slot in hash table */
  78.                 FinChar,                    /* Decompressor variable */
  79.                 BitMask,                    /* AND mask for data size */
  80.                 ReadMask,                /* Code AND mask for current code size */
  81.                 Misc;                               /* miscellaneous bits (interlace, local cmap)*/
  82.  
  83.     id            pic;
  84.  
  85.     BOOL        Interlace, 
  86.                 HasColormap;
  87.  
  88.     unsigned char    *RawGIF;                /* The heap array to hold it, raw */
  89.     unsigned char    *Raster;                    /* The raster data stream, unblocked */
  90.     unsigned char    *picPtrSave;                /* The raster data stream, unblocked */
  91.  
  92.     /* The hash table used by the decompressor */
  93.     int            Prefix[4096];
  94.     int            Suffix[4096];
  95.  
  96.     /* An output array used by the decompressor */
  97.     int            OutCode[1025];
  98.  
  99.     char            *gifID = "GIF87a";
  100.     long            filesize;
  101.     unsigned char    r[256], g[256], b[256];      /* colormap */
  102.     int            DEBUG;                         /* print debugging info */
  103.     float            normaspect;                /* normal aspect ratio of this picture */  
  104.  
  105. static void putword(int, NXStream *);
  106. static void compress(int, NXStream *, byte *, int);
  107. static void output(int);
  108. static void cl_block(void);
  109. static void cl_hash(count_int);
  110. static void char_init(void);
  111. static void char_out(int);
  112. static void flush_char(void);
  113. int WriteGIF(    NXStream *fp, byte *pic, int w, int h, byte *rmap, byte *gmap, byte *bmap, int numcols, int colorstyle);
  114.  
  115. static int        curError;
  116. static int     errState;
  117.  
  118. - init
  119. {
  120.     curError = ERROR_NO_ERROR;
  121.     errState = CONVERT_ERR_NONE;
  122.  
  123.     return self;
  124. }
  125.  
  126. - free
  127. {
  128.     return self;
  129. }
  130.  
  131. - readFromStream: (NXStream *)stream from: sender;
  132. {
  133.     id        retVal;
  134.     
  135.     retVal = ReadGIF(stream, 1);
  136.     if (!retVal) {
  137.         errState = CONVERT_ERR_FATAL;
  138.         curError = GIFError();
  139.     }
  140.     return retVal;
  141. }
  142.  
  143. - (BOOL)write: (id)image toStream: (NXStream *)stream from: sender;
  144. {
  145.     id                imageCon = [sender getImageControl: image];
  146.     unsigned char        *data;
  147.     unsigned char        r[256], g[256], b[256];
  148.     
  149. #ifdef DEBUG
  150.     fprintf(stderr, "Mapping to Palette...\n");
  151. #endif
  152.     [imageCon     convertToPalette:    256
  153.                 andReturn:        &data
  154.                 andPalettes:        r : g : b ];
  155.  
  156.     return WriteGIF(    stream, data, [image pixelsWide], [image pixelsHigh],
  157.                     r, g, b, 256, 0);
  158. }
  159.  
  160. - readAllFromStream: (NXStream *)stream from: sender;
  161. {
  162.     return nil;
  163. }
  164.  
  165. - (BOOL)writeAll: (id)image toStream: (NXStream *)stream;
  166. {
  167.     return NO;
  168. }
  169.  
  170. - customSaveView: (int)width;
  171. {
  172.     return nil;
  173. }
  174.  
  175. - customOpenView: (int)width
  176. {
  177.     return nil;
  178. }
  179.  
  180. - (char *)getFormatName;
  181. {
  182.     return "GIF (Graphics Interchange Format)";
  183. }
  184.  
  185.  - (BOOL)setCustomParameter: (const char *)parameter withValue: (void *)ptr
  186. {
  187.     return NO;
  188. }
  189.  
  190.  - (void *)getCustomParameter: (const char *)parameter
  191. {
  192.     return nil;
  193. }
  194.  
  195. - (char *)copyrightNotice
  196. {
  197.     return "GIF Converter\nby Alex Raftis\n\nCopyright (c) 1991 Cal Poly State University\nCopyright (c) 1989, 1990 University of Pennsylvania\nCopyright (c) 1988, 1989 by Patrick J. Naughton\nCopyright (c) 1990, David Koblas\n\nEmail bugs to alex@data.ACS.CalPoly.EDU";
  198. }
  199.  
  200. - (int)errorState
  201. {
  202.     return errState;
  203. }
  204.  
  205. - (int)errorMessage
  206. {
  207.     return curError;
  208. }
  209.  
  210. - (char *)errorStringMessage
  211. {
  212.     return NULL;
  213. }
  214.  
  215. - (BOOL)needsWindowServer;
  216. {
  217.     return NO;
  218. }
  219.  
  220. - (char *)protocolVersion
  221. {
  222.     return "1.0";
  223. }
  224.  
  225. /*
  226.  * xvgifwr.c  -  handles writing of GIF files.  based on flgife.c and
  227.  *               flgifc.c from the FBM Library, by Michael Maudlin
  228.  *
  229.  * Contains: 
  230.  *   WriteGIF(fp, pic, w, h, rmap, gmap, bmap, numcols, colorstyle)
  231.  *
  232.  * Note: slightly brain-damaged, in that it'll only write non-interlaced 
  233.  *       GIF files (in the interests of speed, or something)
  234.  *
  235.  */
  236.  
  237.  
  238.  
  239. /*****************************************************************
  240.  * Portions of this code Copyright (C) 1989 by Michael Mauldin.
  241.  * Permission is granted to use this file in whole or in part provided
  242.  * that you do not sell it for profit and that this copyright notice
  243.  * and the names of all authors are retained unchanged.
  244.  *
  245.  * Authors:  Michael Mauldin (mlm@cs.cmu.edu)
  246.  *           David Rowley (mgardi@watdcsu.waterloo.edu)
  247.  *
  248.  * Based on: compress.c - File compression ala IEEE Computer, June 1984.
  249.  *
  250.  *    Spencer W. Thomas       (decvax!harpo!utah-cs!utah-gr!thomas)
  251.  *    Jim McKie               (decvax!mcvax!jim)
  252.  *    Steve Davies            (decvax!vax135!petsd!peora!srd)
  253.  *    Ken Turkowski           (decvax!decwrl!turtlevax!ken)
  254.  *    James A. Woods          (decvax!ihnp4!ames!jaw)
  255.  *    Joe Orost               (decvax!vax135!petsd!joe)
  256.  *****************************************************************/
  257.  
  258. /*
  259.  * Copyright 1989, 1990 by the University of Pennsylvania
  260.  *
  261.  * Permission to use, copy, and distribute for non-commercial purposes,
  262.  * is hereby granted without fee, providing that the above copyright
  263.  * notice appear in all copies and that both the copyright notice and this
  264.  * permission notice appear in supporting documentation.
  265.  *
  266.  * The software may be modified for your own purposes, but modified versions
  267.  * may not be distributed.
  268.  *
  269.  * This software is provided "as is" without any express or implied warranty.
  270.  */
  271.  
  272. /*************************************************************/
  273. int WriteGIF(    NXStream    *fp, 
  274.             byte         *pic, 
  275.             int             w, 
  276.             int             h, 
  277.             byte         *rmap, 
  278.             byte         *gmap, 
  279.             byte         *bmap, 
  280.             int             numcols, 
  281.             int             colorstyle)
  282. {
  283.     int        RWidth, RHeight;
  284.     int        LeftOfs, TopOfs;
  285.     int        Resolution, ColorMapSize, InitCodeSize, Background, BitsPerPixel;
  286.     int        i,j;
  287.  
  288.  
  289.     /* if writing B/W stipple... */
  290.     if (colorstyle==2) {
  291.         rmap = gmap = bmap = bw;
  292.         numcols = 2;
  293.     }
  294.  
  295.     Interlace = 0;
  296.     Background = 0;
  297.  
  298.     /* figure out 'BitsPerPixel' */
  299.     for (i=1; i<8; i++)
  300.         if ( (1<<i) >= numcols) break;
  301.   
  302.     BitsPerPixel = i;
  303.  
  304.     ColorMapSize = 1 << BitsPerPixel;
  305.     
  306.     RWidth  = Width  = w;
  307.     RHeight = Height = h;
  308.     LeftOfs = TopOfs = 0;
  309.     
  310.     Resolution = BitsPerPixel;
  311.  
  312.     CountDown = w * h;                    /* # of pixels we'll be doing */
  313.  
  314.     if (BitsPerPixel <= 1) InitCodeSize = 2;
  315.     else InitCodeSize = BitsPerPixel;
  316.  
  317.     curx = cury = 0;
  318.  
  319.     if (!fp) {
  320. #ifdef DEBUG
  321.         fprintf(stderr,  "WriteGIF: file not open for writing\n" );
  322. #endif
  323.         return (0);
  324.     }
  325.  
  326. #ifdef DEBUG
  327.     fprintf(stderr,"WrGIF: pic=%lx, w,h=%dx%d, numcols=%d, Bits%d,Cmap=%d\n",
  328.                             pic, w,h,numcols,BitsPerPixel,ColorMapSize);
  329. #endif
  330.  
  331.     NXWrite(fp, "GIF87a", 6);                /* the GIF magic number */
  332.  
  333.     putword(RWidth, fp);                    /* screen descriptor */
  334.     putword(RHeight, fp);
  335.  
  336.     i = 0x80;                                /* Yes, there is a color map */
  337.     i |= (8-1)<<4;                            /* OR in the color resolution (hardwired 8) */
  338.     i |= (BitsPerPixel - 1);                    /* OR in the # of bits per pixel */
  339.     NXPutc(fp,i);
  340.  
  341.     NXPutc(fp, Background);                /* background color */
  342.  
  343.     NXPutc(fp, 0);                        /* future expansion byte */
  344.  
  345.  
  346.     if (colorstyle == 1) {                    /* greyscale */
  347.         for (i = 0; i < ColorMapSize; i++) {
  348.             j = MONO(rmap[i], gmap[i], bmap[i]);
  349.                 NXPutc(fp, j);
  350.                 NXPutc(fp, j);
  351.                 NXPutc(fp, j);
  352.             }
  353.         }
  354.     else {
  355.         for (i=0; i<ColorMapSize; i++) {        /* write out Global colormap */
  356.             NXPutc(fp, rmap[i]);
  357.             NXPutc(fp, gmap[i]);
  358.             NXPutc(fp, bmap[i]);
  359.         }
  360.     }
  361.  
  362.     NXPutc( fp, ',' );                        /* image separator */
  363.  
  364.     /* Write the Image header */
  365.     putword(LeftOfs, fp);
  366.     putword(TopOfs,  fp);
  367.     putword(Width,   fp);
  368.     putword(Height,  fp);
  369.     if (Interlace) NXPutc(fp, 0x40);            /* Use Global Colormap, maybe Interlace */
  370.     else NXPutc(fp, 0x00);
  371.  
  372.     NXPutc(fp, InitCodeSize);
  373.     compress(InitCodeSize+1, fp, pic, w*h);
  374.  
  375.     NXPutc(fp, 0);                        /* Write out a Zero-length packet (EOF) */
  376.     NXPutc(fp, ';');                        /* Write GIF file terminator */
  377.  
  378.     return (1);
  379. }
  380.  
  381. /******************************/
  382. static void putword(int w, NXStream *fp)
  383. {
  384.     /* writes a 16-bit integer in GIF order (LSB first) */
  385.     NXPutc(fp, w & 0xff);
  386.     NXPutc(fp, (w>>8)&0xff);
  387. }
  388.  
  389.  
  390.  
  391. /***********************************************************************/
  392. static unsigned long    cur_accum    = 0;
  393. static int                cur_bits        = 0;
  394.  
  395. #define min(a,b)        ((a>b) ? b : a)
  396.  
  397. #define BITS            12
  398. #define MSDOS        1
  399.  
  400. #define HSIZE        5003                /* 80% occupancy */
  401.  
  402. typedef unsigned char    char_type;
  403.  
  404.  
  405. static int                n_bits;                /* number of bits/code */
  406. static int                maxbits        = BITS;    /* user settable max # bits/code */
  407. static int                maxcode;            /* maximum code, given n_bits */
  408. static int                maxmaxcode     = 1 << BITS;   /* NEVER generate this */
  409.  
  410. #define MAXCODE(n_bits)     ( (1 << (n_bits)) - 1)
  411.  
  412. static  count_int        htab [HSIZE];
  413. static  unsigned short    codetab [HSIZE];
  414. #define HashTabOf(i)    htab[i]
  415. #define CodeTabOf(i)    codetab[i]
  416.  
  417. static int                hsize        = HSIZE; /* for dynamic table sizing */
  418.  
  419. /*
  420.  * To save much memory, we overlay the table used by compress() with those
  421.  * used by decompress().  The tab_prefix table is the same size and type
  422.  * as the codetab.  The tab_suffix table needs 2**BITS characters.  We
  423.  * get this from the beginning of htab.  The output stack uses the rest
  424.  * of htab, and contains characters.  There is plenty of room for any
  425.  * possible stack (stack used to be 8000 characters).
  426.  */
  427.  
  428. #define tab_prefixof(i)    CodeTabOf(i)
  429. #define tab_suffixof(i)    ((char_type *)(htab))[i]
  430. #define de_stack        ((char_type *)&tab_suffixof(1<<BITS))
  431.  
  432. static int             free_ent         = 0;        /* first unused entry */
  433.  
  434. /*
  435.  * block compression parameters -- after all codes are used up,
  436.  * and compression rate changes, start over.
  437.  */
  438. static int                clear_flg        = 0;
  439.  
  440. static long int            in_count         = 1;        /* length of input */
  441. static long int         out_count     = 0;        /* # of codes output (for debugging) */
  442.  
  443. /*
  444.  * compress stdin to stdout
  445.  *
  446.  * Algorithm:  use open addressing double hashing (no chaining) on the 
  447.  * prefix code / next character combination.  We do a variant of Knuth's
  448.  * algorithm D (vol. 3, sec. 6.4) along with G. Knott's relatively-prime
  449.  * secondary probe.  Here, the modular division first probe is gives way
  450.  * to a faster exclusive-or manipulation.  Also do block compression with
  451.  * an adaptive reset, whereby the code table is cleared when the compression
  452.  * ratio decreases, but after the table fills.  The variable-length output
  453.  * codes are re-sized at this point, and a special CLEAR code is generated
  454.  * for the decompressor.  Late addition:  construct the table according to
  455.  * file size for noticeable speed improvement on small files.  Please direct
  456.  * questions about this implementation to ames!jaw.
  457.  */
  458.  
  459. static int            g_init_bits;
  460. static NXStream    *g_outfile;
  461.  
  462. /********************************************************/
  463. static void compress(int init_bits, NXStream *outfile, byte *data, int   len)
  464. {
  465.     register long        fcode;
  466.     register int         i = 0;
  467.     register int        c;
  468.     register int        ent;
  469.     register int        disp;
  470.     register int        hsize_reg;
  471.     register int        hshift;
  472.  
  473.     /*
  474.      * Set up the globals:  g_init_bits - initial number of bits
  475.      *                      g_outfile   - pointer to output file
  476.      */
  477.     g_init_bits    = init_bits;
  478.     g_outfile        = outfile;
  479.  
  480.     /* initialize 'compress' globals */
  481.     maxbits = BITS;
  482.     maxmaxcode = 1<<BITS;
  483.     memset((char *) htab, 0, sizeof(htab));
  484.     memset((char *) codetab, 0, sizeof(codetab));
  485.     hsize = HSIZE;
  486.     free_ent = 0;
  487.     clear_flg = 0;
  488.     in_count = 1;
  489.     out_count = 0;
  490.     cur_accum = 0;
  491.     cur_bits = 0;
  492.  
  493.       /*
  494.        * Set up the necessary values
  495.        */
  496.     out_count = 0;
  497.     clear_flg = 0;
  498.     in_count = 1;
  499.     maxcode = MAXCODE(n_bits = g_init_bits);
  500.  
  501.     ClearCode = (1 << (init_bits - 1));
  502.     EOFCode = ClearCode + 1;
  503.     free_ent = ClearCode + 2;
  504.  
  505.     char_init();
  506.     ent = *data++;  len--;
  507.  
  508.     hshift = 0;
  509.     for ( fcode = (long) hsize;  fcode < 65536L; fcode *= 2L )
  510.         hshift++;
  511.     hshift = 8 - hshift;                        /* set hash code range bound */
  512.  
  513.     hsize_reg = hsize;
  514.     cl_hash( (count_int) hsize_reg);            /* clear hash table */
  515.  
  516.     output(ClearCode);
  517.     
  518.     while (len) {
  519.         c = *data++;  len--;
  520.         in_count++;
  521.  
  522.         fcode = (long) ( ( (long) c << maxbits) + ent);
  523.         i = (((int) c << hshift) ^ ent);    /* xor hashing */
  524.  
  525.         if ( HashTabOf (i) == fcode ) {
  526.             ent = CodeTabOf (i);
  527.             continue;
  528.         }
  529.         else if ( (long)HashTabOf (i) < 0 )    /* empty slot */
  530.             goto nomatch;
  531.  
  532.         disp = hsize_reg - i;                /* secondary hash (after G. Knott) */
  533.         if ( i == 0 ) disp = 1;
  534.  
  535. probe:
  536.         if ( (i -= disp) < 0 ) i += hsize_reg;
  537.  
  538.         if ( HashTabOf (i) == fcode ) {
  539.             ent = CodeTabOf (i);
  540.             continue;
  541.         }
  542.  
  543.         if ( (long)HashTabOf (i) > 0 ) 
  544.             goto probe;
  545.  
  546. nomatch:
  547.         output(ent);
  548.         out_count++;
  549.         ent = c;
  550.  
  551.         if ( free_ent < maxmaxcode ) {
  552.             CodeTabOf (i) = free_ent++; /* code -> hashtable */
  553.             HashTabOf (i) = fcode;
  554.         } 
  555.         else cl_block();
  556.     }
  557.  
  558.     /* Put out the final code */
  559.     output(ent);
  560.     out_count++;
  561.     output(EOFCode);
  562. }
  563.  
  564.  
  565. /*****************************************************************
  566.  * TAG( output )
  567.  *
  568.  * Output the given code.
  569.  * Inputs:
  570.  *    code:    A n_bits-bit integer.  If == -1, then EOF.  This assumes
  571.  *            that n_bits =< (long)wordsize - 1.
  572.  * Outputs:
  573.  *    Outputs code to the file.
  574.  * Assumptions:
  575.  *    Chars are 8 bits long.
  576.  * Algorithm:
  577.  *    Maintain a BITS character long buffer (so that 8 codes will
  578.  *     fit in it exactly).  Use the VAX insv instruction to insert each
  579.  *    code in turn.  When the buffer fills up empty it and start over.
  580.  */
  581.  
  582. static unsigned long masks[] = { 0x0000, 0x0001, 0x0003, 0x0007, 0x000F,
  583.                                   0x001F, 0x003F, 0x007F, 0x00FF,
  584.                                   0x01FF, 0x03FF, 0x07FF, 0x0FFF,
  585.                                   0x1FFF, 0x3FFF, 0x7FFF, 0xFFFF };
  586.  
  587. static void output(int code)
  588. {
  589.     cur_accum &= masks[cur_bits];
  590.  
  591.     if (cur_bits > 0)
  592.         cur_accum |= ((long)code << cur_bits);
  593.     else
  594.         cur_accum = code;
  595.     
  596.     cur_bits += n_bits;
  597.  
  598.     while( cur_bits >= 8 ) {
  599.         char_out( (unsigned int) (cur_accum & 0xff) );
  600.         cur_accum >>= 8;
  601.         cur_bits -= 8;
  602.     }
  603.  
  604.     /*
  605.       * If the next entry is going to be too big for the code size,
  606.        * then increase it, if possible.
  607.        */
  608.  
  609.     if (free_ent > maxcode || clear_flg) {
  610.         if( clear_flg ) {
  611.             maxcode = MAXCODE (n_bits = g_init_bits);
  612.             clear_flg = 0;
  613.         } else {
  614.             n_bits++;
  615.             if ( n_bits == maxbits )
  616.                 maxcode = maxmaxcode;
  617.             else
  618.                 maxcode = MAXCODE(n_bits);
  619.         }
  620.     }
  621.     
  622.     if( code == EOFCode ) {
  623.         /* At EOF, write the rest of the buffer */
  624.         while( cur_bits > 0 ) {
  625.             char_out( (unsigned int)(cur_accum & 0xff) );
  626.             cur_accum >>= 8;
  627.             cur_bits -= 8;
  628.         }
  629.  
  630.         flush_char();
  631.     
  632.         NXFlush( g_outfile );
  633.  
  634. //        if( NXError( g_outfile ) )
  635. //            FatalError("unable to write GIF file");
  636.     }
  637. }
  638.  
  639.  
  640. /********************************/
  641. static void    cl_block(void)                /* table clear for block compress */
  642. {
  643.     /* Clear out the hash table */
  644.  
  645.     cl_hash ( (count_int) hsize );
  646.     free_ent = ClearCode + 2;
  647.     clear_flg = 1;
  648.  
  649.     output(ClearCode);
  650. }
  651.  
  652.  
  653. /********************************/
  654. static void cl_hash(register count_int hsize)    /* reset code table */
  655. {
  656.     register count_int    *htab_p    = htab+hsize;
  657.     register long        i;
  658.     register long        m1        = -1;
  659.  
  660.     i = hsize - 16;
  661.     do {                                    /* might use Sys V memset(3) here */
  662.         *(htab_p-16) = m1;
  663.         *(htab_p-15) = m1;
  664.         *(htab_p-14) = m1;
  665.         *(htab_p-13) = m1;
  666.         *(htab_p-12) = m1;
  667.         *(htab_p-11) = m1;
  668.         *(htab_p-10) = m1;
  669.         *(htab_p-9) = m1;
  670.         *(htab_p-8) = m1;
  671.         *(htab_p-7) = m1;
  672.         *(htab_p-6) = m1;
  673.         *(htab_p-5) = m1;
  674.         *(htab_p-4) = m1;
  675.         *(htab_p-3) = m1;
  676.         *(htab_p-2) = m1;
  677.         *(htab_p-1) = m1;
  678.         htab_p -= 16;
  679.     } while ((i -= 16) >= 0);
  680.  
  681.     for ( i += 16; i > 0; i-- )
  682.         *--htab_p = m1;
  683. }
  684.  
  685.  
  686. /******************************************************************************
  687.  *
  688.  * GIF Specific routines
  689.  *
  690.  ******************************************************************************/
  691.  
  692. /*
  693.  * Number of characters so far in this 'packet'
  694.  */
  695. static int        a_count;
  696.  
  697. /*
  698.  * Set up the 'byte output' routine
  699.  */
  700. static void char_init()
  701. {
  702.     a_count = 0;
  703. }
  704.  
  705. /*
  706.  * Define the storage for the packet accumulator
  707.  */
  708. static char accum[ 256 ];
  709.  
  710. /*
  711.  * Add a character to the end of the current packet, and if it is 254
  712.  * characters, flush the packet to disk.
  713.  */
  714. static void char_out(int c)
  715. {
  716.     accum[ a_count++ ] = c;
  717.     if( a_count >= 254 ) 
  718.         flush_char();
  719. }
  720.  
  721. /*
  722.  * Flush the packet to disk, and reset the accumulator
  723.  */
  724. static void flush_char()
  725. {
  726.     if( a_count > 0 ) {
  727.         NXPutc( g_outfile, a_count );
  728.         NXWrite(g_outfile, accum, a_count);
  729.         a_count = 0;
  730.     }
  731. }    
  732.  
  733. @end
  734.